package com.dx.login;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

public class LoginController  extends Controller{
	public void index() {	
		//已登录则跳转到登陆成功页面
		if(this.getSession().getAttribute("user")!=null)
			redirect("/");
		else {
			render("/WEB-INF/jsp/login/login.jsp");
		}
	}
	
	@Before(LoginValidator.class)
	public void tologin(){
		Object[] params=new Object[2];
		params[0]=this.getPara("account");
		params[1]=this.getPara("password");
		//System.out.println("counts:"+User.dao.find("select count(1) counts from user where account=? and password=?", params).get(0).getLong("counts"));
		if(User.dao.find("select count(1) counts from user where account=? and password=?", params).get(0).getLong("counts")==1L){
			User user=User.dao.findById(this.getPara("account"));
			this.getSession().setAttribute("user", user);	
			System.out.println("continue_url:::::"+this.getSession().getAttribute("continue_url"));
			if(!StringUtils.isBlank((String)this.getSession().getAttribute("continue_url"))){
				redirect(this.getSession().getAttribute("continue_url").toString());
			}else {
				redirect("/");
			}
			
		}else{
			render("/WEB-INF/jsp/login/login.jsp");
		}
	}
	
	public void logout(){
		this.getSession().removeAttribute("user");		
		redirect("/");

	}
}
